package ru.st.selenium.pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class NewMoviePage extends Page {

    @FindBy(xpath = "//input[@name='imdbid']")
    private WebElement imdbNumberInput;

    @FindBy(xpath = "//input[@name='name']")
    private WebElement titleInput;

    @FindBy(xpath = "//textarea[@name='aka']")
    private WebElement alsoKnownAsInput;

    @FindBy(xpath = "//input[@name='year']")
    private WebElement yearInput;

    @FindBy(xpath = "//input[@name='duration']")
    private WebElement durationInput;

    @FindBy(xpath = "//input[@id='submit']")
    private WebElement submitBtn;

    @FindBy(xpath = "//input[@name='year']/ancestor::td/label[@for='year']")
    private WebElement yearAlert;

    public WebElement getYearAlert() {
        return yearAlert;
    }

    public  NewMoviePage(WebDriver webDriver) {
        super(webDriver);
    }

    public NewMoviePage setTitle(String title) {
        titleInput.clear();
        titleInput.sendKeys(title);
        return this;
    }

    public NewMoviePage setImdbNumber(String imbd) {
        imdbNumberInput.clear();
        imdbNumberInput.sendKeys(imbd);
        return this;
    }

    public NewMoviePage setAlsoKnown(String alsoKnown) {
        alsoKnownAsInput.clear();
        alsoKnownAsInput.sendKeys(alsoKnown);
        return this;
    }

    public NewMoviePage setYear(String year) {
        yearInput.clear();
        yearInput.sendKeys(year);
        return this;
    }

    public NewMoviePage setDuration(String duration) {
        durationInput.clear();
        durationInput.sendKeys(duration);
        return this;
    }

    public SimpleMoviePage clickSubmit() {
        submitBtn.click();
        return PageFactory.initElements(getWebDriver(), SimpleMoviePage.class);
    }

    public SimpleMoviePage addNewMovieToCollection(String title, String also, String year, String duration) {
        return setTitle(title)
//                .setImdbNumber(imdb)
                .setAlsoKnown(also)
                .setYear(year)
                .setDuration(duration)
                .clickSubmit();
    }
}
